package com.veo.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.veo.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
//全局服务降级
@DefaultProperties(defaultFallback = "globalFallBackMethod")
public class OrderController {

    @Resource
    private OrderService orderService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public  String paymentOk(@PathVariable("id")Integer id){
        return orderService.paymentOk(id);
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    //局部服务降级（耦合度高，代码冗余，业务逻辑混淆，异常代码和业务代码）
//    @HystrixCommand(fallbackMethod = "paymentTimeoutFallBackMethod",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
//    })
    //全局服务降级
    @HystrixCommand
    public  String paymentTimeout(@PathVariable("id")Integer id){
        return orderService.paymentTimeout(id);
    }

    public  String paymentTimeoutFallBackMethod(@PathVariable("id")Integer id){
        return "我是消费者80，对方支付系统繁忙请10秒钟后试试看或者检查自己是否运行出错o(╥﹏╥)o";
    }

    public  String globalFallBackMethod(){
        return "global异常处理信息，请稍后再试！";
    }
}
